package ua.kiev.tosik.price.gui;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.jdesktop.swingx.JXTitledPanel;
import ua.kiev.tosik.price.Main;
import ua.kiev.tosik.price.conf.Chapter;
import ua.kiev.tosik.price.conf.ChapterConfig;
import ua.kiev.tosik.price.conf.Language;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TaskMenuPanel  {

	private static TaskMenuPanel ourInstance;
	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.gui");
	private JXTitledPanel taskPanel;
	private JScrollPane scrollPane;
	private JXTaskPaneContainer tpc;
	private JXTaskPane infoTaskPane;
	private JXTaskPane chapterPane;

	private Language lang;
	private JXLabel profile;
	private JXLabel sessionTime;

	private JXLabel userName = new JXLabel();
	private JXLabel sessionTimeValue = new JXLabel();
	private JXLabel countMessageValue = new JXLabel();
	private JXLabel calendarValue = new JXLabel();
	private JXLabel calcValue = new JXLabel();

	private DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
	private DateFormat dateFormatYear = new SimpleDateFormat("yyyy");

	public static TaskMenuPanel getInstance() {
		if (ourInstance == null) {
			ourInstance = new TaskMenuPanel();
		}
		return ourInstance;
	}

	private TaskMenuPanel() {
		logger.debug("Initialize task menu panel");
		lang = Language.getInstance();
		taskPanel = new JXTitledPanel();
		tpc = new JXTaskPaneContainer();
		createTaskPane(tpc);
		scrollPane = new JScrollPane(tpc, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		taskPanel.add(scrollPane);
		updateLocale();
		userName.setText("Test user");
		Timer tm = new Timer(5000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				sessionTimeValue.setText(Main.getAppWorkTime(new Date().getTime()));
			}
		});
		tm.start();
	}

	private void createTaskPane(JXTaskPaneContainer tpc) {
		infoTaskPane = new JXTaskPane();
		infoTaskPane.setLayout(new GridLayout(5, 2));
		createInfoTaskPane(infoTaskPane);

		chapterPane = new JXTaskPane();
		createChapterPane(chapterPane);

		tpc.add(infoTaskPane);
		tpc.add(chapterPane);
	}

	private void createChapterPane(JXTaskPane chapterPane) {
		for (final Chapter ch : ChapterConfig.getInstance().getChapterList()) {
			Action chAction = new AbstractAction() {
				{
					putValue(Action.NAME, ch.getName());
					putValue(Action.SHORT_DESCRIPTION, ch.getName());
					putValue(Action.SMALL_ICON, ch.getImageSmall());
				}

				public void actionPerformed(ActionEvent e) {
					ChapterRunner.runChapter(ch);
				}
			};
			chapterPane.add(chAction);
		}
	}

	private void createInfoTaskPane(JXTaskPane infoTaskPane) {
		profile = new JXLabel();
		sessionTime = new JXLabel();
		Action messageTask = new AbstractAction() {
			{
				putValue(Action.NAME, lang.get("app.taskpanel.info.message"));
				putValue(Action.SHORT_DESCRIPTION, lang.get("app.taskpanel.info.message"));
				// putValue(Action.SMALL_ICON, Images.NetworkConnected.getIcon(32, 32));
				putValue(Action.SMALL_ICON, new ImageIcon(lang.get("app.taskpanel.info.message.icon")));
			}

			public void actionPerformed(ActionEvent e) {
				// label.setText("an action performed");
			}
		};
		Action calendarTask = new AbstractAction() {
			{
				putValue(Action.NAME, lang.get("app.taskpanel.info.calendar"));
				putValue(Action.SHORT_DESCRIPTION, lang.get("app.taskpanel.info.calendar"));
				putValue(Action.SMALL_ICON, new ImageIcon(lang.get("app.taskpanel.info.calendar.icon")));
			}

			public void actionPerformed(ActionEvent e) {
				new CalendarWindow(dateFormatYear.format(new Date()));
			}
		};
		Action calcTask = new AbstractAction() {
			{
				putValue(Action.NAME, lang.get("app.taskpanel.info.calc"));
				putValue(Action.SHORT_DESCRIPTION, lang.get("app.taskpanel.info.calc"));
				putValue(Action.SMALL_ICON, new ImageIcon(lang.get("app.taskpanel.info.calc.icon")));
			}

			public void actionPerformed(ActionEvent e) {
				try {
					Runtime.getRuntime().exec("calc");
				} catch (IOException e1) {
					logger.error(e1.getMessage(), e1);
				}
			}
		};
		infoTaskPane.add(profile);
		infoTaskPane.add(userName);
		infoTaskPane.add(sessionTime);
		infoTaskPane.add(sessionTimeValue);
		infoTaskPane.add(messageTask);
		infoTaskPane.add(countMessageValue);
		infoTaskPane.add(calendarTask);
		infoTaskPane.add(calendarValue);
		infoTaskPane.add(calcTask);
		infoTaskPane.add(calcValue);
	}

	public JXTitledPanel getTaskPanel() {
		return taskPanel;
	}

	public void updateLocale() {
		taskPanel.setTitle(lang.get("app.taskpanel.title"));
		infoTaskPane.setTitle(lang.get("app.taskpanel.info.title"));
		infoTaskPane.setIcon(new ImageIcon(lang.get("app.taskpanel.info.title.icon")));
		profile.setText(lang.get("app.taskpanel.info.user") + ": ");
		profile.setIcon(new ImageIcon(lang.get("app.taskpanel.info.user.icon")));

		sessionTime.setText(lang.get("app.taskpanel.info.session") + ": ");
		sessionTime.setIcon(new ImageIcon(lang.get("app.taskpanel.info.session.icon")));
		calendarValue.setText(dateFormat.format(new Date()));

		chapterPane.setTitle(lang.get("app.taskpanel.chapter.title"));
		chapterPane.setIcon(new ImageIcon(lang.get("app.taskpanel.chapter.title.icon")));
	}
}
